package cn.zhuama.org.service.impl;

import cn.zhuama.basic.query.BaseQuery;
import cn.zhuama.basic.sevice.impl.BaseServiceImpl;
import cn.zhuama.basic.util.PageList;
import cn.zhuama.org.domain.Department;
import cn.zhuama.org.domain.Employee;
import cn.zhuama.org.mapper.DepartmentMapper;
import cn.zhuama.org.query.DepartmentQuery;
import cn.zhuama.org.service.IDepartmentService;
import com.sun.corba.se.impl.encoding.EncapsOutputStream;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;

@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class IDepartmentServiceImpl  extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        //如果没有选择上级部门
        if(department.getParent()==null || department.getParent().getId()==null){
            //只添加自己的id - 自增长id
            department.setDirPath("/"+department.getId());
        }else{
            //获取上级部门id
            Long parent_id = department.getParent().getId();
            //获取上级部门的dirPath
            String parent_dirPath = departmentMapper.queryOne(parent_id).getDirPath();
            //组装自己的id
            department.setDirPath(parent_dirPath+"/"+department.getId());
        }
        //更新到数据库
        departmentMapper.update(department);
    }

//    @Override
//    @Transactional
//    public void del(Long id) {
//        departmentMapper.del(id);
//    }

    @Transactional
    @Override
    public void update(Department department) {
        if(department.getParent()==null || department.getParent().getId()==null){
            department.setDirPath("/"+department.getId());
        }else{
            Long parent_id = department.getParent().getId();
            String parent_dirPath = departmentMapper.queryOne(parent_id).getDirPath();
            //对上级部门的处理
            // /1/47/3      47
            if(parent_dirPath.indexOf(department.getId().toString())!=-1){
                throw new RuntimeException("上级部门不能选择自己");
            }
            department.setDirPath(parent_dirPath+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    public List<Department> deptTree() {
        // 1. 一次性查询出所有数据，放到内存中，可以使用map
        // 2. 从所有数据中找出第一层级/顶级部门：这就是我们需要返回的数据
        // 3. 遍历所有部门只要不是第一层级的，找自己的上级，并将自己放入上级的children中
        //将所有的部门撞到map中  - 每次获取父部门都从这里获取
        List<Department> all = departmentMapper.loadAll();
        System.err.println("这是测试all");
        System.err.println("all");
//        for (Department department : all) {
//            System.err.println(department);
//        }


    Map<Long,Department> map  = new HashMap<>();
        for (Department department : all) {
            map.put(department.getId(),department);//爹key   自己value
        }
        System.err.println("这是测试map");
//        Set<Long> s = map.keySet();
//        for (Long l:s){
//            System.out.println(map.get(l));
//        }
        map.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
        //前端需要得到的集合数据 - 部门树[deptTree一级部门]
        List<Department> deptTree = new ArrayList<>();
        System.err.println("这是测试deptTree --1");

        for (Department department:all){
            if(department.getParent_id() == null){//顶级部门
                deptTree.add(department);
                continue;
            }//不是顶级部门
                //1.想办法找到当前部门的 父部门
                Long parent_id = department.getParent_id();
                //这种从数据库查询的方式，每次通过id去查询 - 查询出来映射给实体domain，children集合中都会是空集合 - 没有任何数据
                //Department parentDepartment = departmentMapper.loadById(parent_id);
                //下面这种是从Map中去获取的 - 以前装进去的对象还是会存在
                Department parentDepartment = map.get(parent_id);
                //2.将自己装到  父部门 的children中
                parentDepartment.getChildren().add(department);
        }
        System.err.println("这是测试deptTree --2");
        deptTree.forEach(System.out::println);
        return deptTree;
    }

//    @Override
//    public PageList<Department> queryPage(DepartmentQuery query) {
//        Long count = departmentMapper.queryCount(query);
//        if(count<1){
//            return new PageList<Department>();
//        }
//        List<Department> list = departmentMapper.queryData(query);
//        return new PageList<Department>(count,list);
//    }
//
//
//    @Override
//    public Department queryOne(Long id) {
//        return departmentMapper.queryOne(id);
//    }
//
//    @Override
//    public List<Department> findAll() {
//        return departmentMapper.loadAll();
//    }
//
//
//
//    @Override
//    @Transactional
//    public void patchDel(List<Long> ids) {
//        departmentMapper.patchDel(ids);
//    }


}
